--[[ 
ESN_BGSound
By EnSabahNur

based on Battleground Sounds from Delthis of Kil'Jaeden Thanks for Codebase 

]]
----------------------------
--      Localization 	 --
----------------------------
local L = GetLocale() == "deDE" and {
	["ESN_BGSound ready."] = " ist bereit.",
	["Allianz"] = "Allianz",
	["Horde"] = "Horde",
	["Sekunden"] = "30 Sekunden",
	["fallen lassen!"] = "fallen lassen!",
	["fallengelassen."] = "fallengelassen.",
	["aufgenommen!"] = "aufgenommen!",
	["aufgenommen."] = "aufgenommen.",
	["errungen"] = "errungen",
	["zur\195\188ckgebracht!"] = "zur\195\188ckgebracht!",
	["Die Horde siegt!"] = "Die Horde siegt!",
	["Die Allianz siegt!"] = "Die Allianz siegt!",
	["Die Horde hat die Flagge erobert!"] = "Die Horde hat die Flagge erobert!",
	["Die Allianz hat die Flagge erobert!"] = "Die Allianz hat die Flagge erobert!",
	["Die Flagge wurde zurckgesetzt."] = "Die Flagge wurde zur\195\188ckgesetzt.",
	["Fifteen seconds"] = "f\195\188nfzehn Sekunden",
	["matchEOTS"] = "Siegespunkte: (%d+)",
	["Warsong Gulch"] = "Kriegshymnenschlucht",
	["Arathi Basin"] = "Arathibecken",
	["Alterac Valley"] = "Alteractal",
	["Eye of the Storm"] = "Auge des Sturms",
} or {
	["ESN_BGSound ready."] = " is ready.",
	["Allianz"] = "Alliance",
	["Horde"] = "Horde",
	["Sekunden"] = "30 seconds",
	["fallen lassen!"] = "dropped",
	["fallengelassen."] = "fallengelassen.",
	["aufgenommen!"] = "picked",
	["aufgenommen."] = "aufgenommen.",
	["errungen"] = "captured",
	["zur\195\188ckgebracht!"] = "returned",
	["Die Horde siegt!"] = "Die Horde siegt!",
	["Die Allianz siegt!"] = "Die Allianz siegt!",
	["Die Horde hat die Flagge erobert!"] = "Die Horde hat die Flagge erobert!",
	["Die Allianz hat die Flagge erobert!"] = "Die Allianz hat die Flagge erobert!",
	["Die Flagge wurde zurckgesetzt."] = "Die Flagge wurde zur\195\188ckgesetzt.",
	["Fifteen seconds"] = "Fifteen seconds",
	["matchEOTS"] = "Victory Points: (%d+)",
	["Warsong Gulch"] = "Warsong Gulch",
	["Arathi Basin"] = "Arathi Basin",
	["Alterac Valley"] = "Alterac Valley",
	["Eye of the Storm"] = "Eye of the Storm",
}

-- Some vars to edit. First var in array is sound length in seconds, second var is file name, and third is place in queue.
--Alliance messages
local B_FLAG_DROPPED = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\bflagdropped.wav"};
local R_FLAG_TAKEN = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\rflagtaken.wav"};
local B_TEAM_SCORES = {3, "Interface\\AddOns\\ESN_BGSound\\sounds\\bteamscores.wav"};
local B_FLAG_RETURNED = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\bflagreturned.wav"};

--Horde messages
local R_FLAG_DROPPED = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\rflagdropped.wav"};
local B_FLAG_TAKEN = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\bflagtaken.wav"};
local R_TEAM_SCORES = {3, "Interface\\AddOns\\ESN_BGSound\\sounds\\rteamscores.wav"};
local R_FLAG_RETURNED = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\rflagreturned.wav"};

--Chat msg system messages
local B_TEAM_WINS = {3, "Interface\\AddOns\\ESN_BGSound\\sounds\\bteamwins.wav"};
local R_TEAM_WINS = {3, "Interface\\AddOns\\ESN_BGSound\\sounds\\rteamwins.wav"};
local FLAG_RETURNED = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\flagreturned.wav"};
local NEW_ROUND_IN = {2, "Interface\\AddOns\\ESN_BGSound\\sounds\\newroundin.wav"};
local THREE = {1, "Interface\\AddOns\\ESN_BGSound\\sounds\\three.wav"};
local TWO = {1, "Interface\\AddOns\\ESN_BGSound\\sounds\\two.wav"};
local ONE = {1, "Interface\\AddOns\\ESN_BGSound\\sounds\\one.wav"};

--Sound queue related stuff, shouldn't need to change
--Many of these are useless and need to be cleaned up
local filesInQueue = 0;
local currentSoundQueue = 0;
local queueTable = {};
local queueActive = 0;
local soundPlaying = 0;
local playStop = 0;
local newRoundIn = 0;

ESN_BGSound = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0");

function ESN_BGSound:OnEnable()
	ESN_BGSound:RegisterEvent("ZONE_CHANGED_NEW_AREA");
	local _, instanceType = IsInInstance()
	if instanceType == "pvp" or "arena" then
		ESN_BGSound:bg_battle();
	end
end

function ESN_BGSound:ZONE_CHANGED_NEW_AREA()
	local _, instanceType = IsInInstance()
	if instanceType == "pvp" or "arena" then
		ESN_BGSound:bg_battle();
	elseif inBG then
		ESN_BGSound:exit_bg();
	end
end

function ESN_BGSound:OnUpdate(arg1)
	if ((playStop ~= 0) and (GetTime() >= playStop)) then
		ESN_BGSound:sound_queue(0);
	end
	if ((newRoundIn ~= 0) and (GetTime() >= newRoundIn)) then
		ESN_BGSound:new_round();
	end
end

function ESN_BGSound:exit_bg()
	if ESN_BGSound:IsEventRegistered("CHAT_MSG_BG_SYSTEM_NEUTRAL") then 
		ESN_BGSound:UnregisterEvent("CHAT_MSG_BG_SYSTEM_NEUTRAL");
	end
	if ESN_BGSound:IsEventRegistered("CHAT_MSG_BG_SYSTEM_ALLIANCE") then
		ESN_BGSound:UnregisterEvent("CHAT_MSG_BG_SYSTEM_ALLIANCE");
	end
	if ESN_BGSound:IsEventRegistered("CHAT_MSG_BG_SYSTEM_HORDE") then
		ESN_BGSound:UnregisterEvent("CHAT_MSG_BG_SYSTEM_HORDE");
	end
	if ESN_BGSound:IsEventRegistered("CHAT_MSG_MONSTER_YELL") then
		ESN_BGSound:UnregisterEvent("CHAT_MSG_MONSTER_YELL");
	end
	if ESN_BGSound:IsEventRegistered("UPDATE_BATTLEFIELD_SCORE") then
		ESN_BGSound:UnregisterEvent("UPDATE_BATTLEFIELD_SCORE");
	end
end

function ESN_BGSound:bg_battle()
	ESN_BGSound:RegisterEvent("CHAT_MSG_BG_SYSTEM_NEUTRAL");
	ESN_BGSound:RegisterEvent("CHAT_MSG_BG_SYSTEM_ALLIANCE");
	ESN_BGSound:RegisterEvent("CHAT_MSG_BG_SYSTEM_HORDE");
	ESN_BGSound:RegisterEvent("CHAT_MSG_MONSTER_YELL");
	ESN_BGSound:RegisterEvent("UPDATE_BATTLEFIELD_SCORE");
end

function ESN_BGSound:CHAT_MSG_BG_SYSTEM_ALLIANCE(msg)
	if (string.find (msg, L["fallen lassen!"]) ~= nil) then
		ESN_BGSound:sound_queue(B_FLAG_DROPPED);
	elseif (string.find (msg, L["fallengelassen."]) ~= nil) then
		ESN_BGSound:sound_queue(B_FLAG_DROPPED);
	elseif (string.find (msg, L["aufgenommen!"]) ~= nil) then
		ESN_BGSound:sound_queue(R_FLAG_TAKEN);
	elseif (string.find (msg, L["errungen"]) ~= nil) then
		ESN_BGSound:sound_queue(B_TEAM_SCORES);
		newRoundIn = GetTime() + 16;
	elseif (string.find (msg, L["zur\195\188ckgebracht!"]) ~= nil) then
		ESN_BGSound:sound_queue(B_FLAG_RETURNED);
	elseif (string.find (msg, L["aufgenommen."]) ~= nil) then
		ESN_BGSound:sound_queue(B_FLAG_TAKEN);	
	elseif (string.find (msg, L["Die Allianz hat die Flagge erobert!"]) ~= nil) then
		ESN_BGSound:sound_queue(B_TEAM_SCORES);	
	else
--	message(L["Allianz"]);
	end
end

function ESN_BGSound:CHAT_MSG_BG_SYSTEM_HORDE(msg)
	if (string.find (msg, L["fallen lassen!"]) ~= nil) then
		ESN_BGSound:sound_queue(R_FLAG_DROPPED);
	elseif (string.find (msg, L["fallengelassen."]) ~= nil) then
		ESN_BGSound:sound_queue(R_FLAG_DROPPED);
	elseif (string.find (msg, L["aufgenommen!"]) ~= nil) then
		ESN_BGSound:sound_queue(B_FLAG_TAKEN);
	elseif (string.find (msg, L["errungen"]) ~= nil) then
		ESN_BGSound:sound_queue(R_TEAM_SCORES);
		newRoundIn = GetTime() + 16;
	elseif (string.find (msg, L["zur\195\188ckgebracht!"]) ~= nil) then
		ESN_BGSound:sound_queue(R_FLAG_RETURNED);
	elseif (string.find (msg, L["aufgenommen."]) ~= nil) then
		ESN_BGSound:sound_queue(R_FLAG_TAKEN);
	elseif (string.find (msg, L["Die Horde hat die Flagge erobert!"]) ~= nil) then
		ESN_BGSound:sound_queue(R_TEAM_SCORES);
	else
--	message(L["Horde"]);
	end
end

function ESN_BGSound:CHAT_MSG_BG_SYSTEM_NEUTRAL(msg)
	if (string.find (msg, L["Sekunden"]) ~= nil) then
		newRoundIn = GetTime() + 26;
	elseif (string.find (msg, L["Fifteen seconds"]) ~= nil) then
		newRoundIn = GetTime() + 11;
	elseif (string.find (msg, L["Die Horde siegt!"]) ~= nil) then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(R_TEAM_WINS);
	elseif (string.find (msg, L["Die Allianz siegt!"]) ~= nil) then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(B_TEAM_WINS);
	elseif (string.find (msg, L["Die Flagge wurde zurckgesetzt."]) ~= nil) then
		ESN_BGSound:sound_queue(FLAG_RETURNED);	
	end
end

function ESN_BGSound:CHAT_MSG_MONSTER_YELL(msg)
	if (string.find (msg, L["Die Horde siegt!"]) ~= nil) then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(R_TEAM_WINS);
	elseif (string.find (msg, L["Die Allianz siegt!"]) ~= nil) then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(B_TEAM_WINS);
	end
end

function ESN_BGSound:UPDATE_BATTLEFIELD_SCORE()
	if (GetZoneText() == L["Eye of the Storm"]) then
	local _, _, texth = GetWorldStateUIInfo(3);
	local _, _, texta = GetWorldStateUIInfo(2);
	local scoreH = texth:match(L["matchEOTS"])
	local scoreA = texta:match(L["matchEOTS"])
	
	scoreH = tonumber(scoreH)
	scoreA = tonumber(scoreA) 
	
	if scoreH >= 2000 then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(R_TEAM_WINS);
	elseif scoreA >= 2000 then
		newRoundIn = 0;
		ESN_BGSound:sound_queue(B_TEAM_WINS);
	end
	else
	end
end

function ESN_BGSound:new_round()
	newRoundIn = 0;
	ESN_BGSound:sound_queue(NEW_ROUND_IN);
	ESN_BGSound:sound_queue(THREE);
	ESN_BGSound:sound_queue(TWO);
	ESN_BGSound:sound_queue(ONE);
end

function ESN_BGSound:sound_queue(sound)
	local currentTime = GetTime();
	if (sound ~= 0) then
		currentspot = 0;
		spotnum = 0;
		while currentspot ~= nil do
			currentspot = queueTable[spotnum + 1];
			spotnum = spotnum + 1;
		end
		queueTable[spotnum] = sound[1];
		queueTable[spotnum + 1] = sound[2];
		queueActive = 1;
	end
	
	if (soundPlaying == 1) then
		if (GetTime() >= playStop) then
			soundPlaying = 0;
			playStop = 0;
		end
	end
	
	if (queueTable[1] ~= nil) then
		if (soundPlaying == 0) then
			PlaySoundFile(queueTable[2]);
			playStop = GetTime() + queueTable[1];
			soundPlaying = 1;
			currentspot = 0;
			spotnum = 0;
			while currentspot ~= nil do
				currentspot = queueTable[spotnum + 1];
				spotnum = spotnum + 1;
			end
				for i = 3, spotnum do
					queueTable[i - 2] = queueTable[i];
				end
			queueTable[spotnum] = nil;
			queueTable[spotnum -1] = nil;
		end
	end
end

--Option Frame
local panel = CreateFrame("Frame")
panel.name = "ESN_BG_Sound"
panel.addon = addon
InterfaceOptions_AddCategory(panel)
	
local b = CreateFrame("Button", "ESN_BG_Sound_Testsound", panel, "UIPanelButtonTemplate")
b:SetWidth(120)
b:SetHeight(20)
b:SetText("Test Sound")
b:SetScript("OnClick", function() ESN_BGSound:sound_queue(NEW_ROUND_IN);end)
b:SetPoint("TOPLEFT", 20, -10)

